package com.lx.screenshot.util;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

public class SettingUtil {
    public static final int TO_LAN_INFO = 0x102;
    public static final int TO_LAN_CODE = 0x103;
    private static final String[] supportLan = new String[]{
            "英文", "中文", "日文", "韩文"
    };
    private static final String TRANS_FROM_KEY = "TRANS_FROM_KEY";
    private static final String TRANS_TO_KEY = "TRANS_TO_KEY";
    public static final String HAD_CHOOSE_AREA = "HAD_CHOOSE_AREA";

    public static String[] getSupportLan() {
        return supportLan;
    }

    public static void setFromLan(Context context, String lan) {
        lan = transSupportLan(lan, TO_LAN_CODE);
        SPUtil.put(context, TRANS_FROM_KEY, lan);
    }

    public static String getFromLan(Context context, String defLan) {
        return SPUtil.get(context, TRANS_FROM_KEY, defLan);
    }

    public static String getFromLan(Context context) {
        return SPUtil.get(context, TRANS_FROM_KEY, "chi_sim");
    }

    public static void setToLan(Context context, String lan) {
        lan = transSupportLan(lan, TO_LAN_CODE);
        SPUtil.put(context, TRANS_TO_KEY, lan);
    }

    public static String getToLan(Context context, String defLan) {
        return SPUtil.get(context, TRANS_TO_KEY, defLan);
    }

    public static String getToLan(Context context) {
        return SPUtil.get(context, TRANS_TO_KEY, "eng");
    }

    public static String transSupportLan(String lan, int type) {
        if (type == TO_LAN_CODE) {
            if ("英文".equals(lan))
                return "eng";
            if ("中文".equals(lan))
                return "chi_sim";
            if ("日文".equals(lan))
                return "jpn";
            if ("韩文".equals(lan))
                return "kor";
        }

        if (type == TO_LAN_INFO) {
            if ("eng".equals(lan))
                return "英文";
            if ("chi_sim".equals(lan))
                return "中文";
            if ("jpn".equals(lan))
                return "日文";
            if ("kor".equals(lan))
                return "韩文";
        }

        return lan;
    }

    public static String getSetting(Context context, String key, String defV) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        String str = sp.getString(key, defV);
        if (str.isEmpty())
            return defV;
        return str;
    }

    public static boolean getSetting(Context context, String key, boolean defV) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        return sp.getBoolean(key, defV);
    }

    public static void putSetting(Context context, String key, String value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putString(key, value).apply();
    }

    public static void putSetting(Context context, String key, boolean value) {
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
        sp.edit().putBoolean(key, value).apply();
    }
}
